let mysql = require("mysql");
let server = require("express")();
const port = 81
server.all("*", function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "content-type");
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200);
    else
        next();
})

let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'chuanqi'
});

sql.connect();

server.get("/anliliebiao", function(request, response) {
    sql.query(`SELECT * FROM chuanqi`, function(error, data) {
        if (error) {
            console.log(error)
            response.end("error")
            return;
        }
        response.end(JSON.stringify(data));
        // console.log(JSON.stringify(data))
    })
})

// server.get("/jjfaliebiao", function(request, response) {
// let id = request.query.id;
server.get("/jjfaliebiao", function(request, response) {
        sql.query(`select * from fangan`, function(error, result) {
            if (error) {
                console.log(error);
                response.end("error");
                return;
            }
            response.end(JSON.stringify(result));
            // console.log(JSON.stringify(result))
        })
    })
    // })


let id = 0
server.get("/demo", function(request, response) {
    id = request.query.id
    server.get("/demo-xq", function(request, response) {
        console.log(id)
        sql.query(`select * from fangan where id=${id}`, function(error, data) {
            if (error) {
                console.log(error);
                response.end("error");
                return;
            }
            response.end(JSON.stringify(data));
        })
    })
})

let idi = 0
server.get("/newdemo", function(request, response) {
    idi = request.query.id;
    server.get("/demo-jjxq", function(request, response) {
        sql.query(`select * from chuanqi where id=${idi}`, function(error, data) {
            if (error) {
                console.log(error);
                response.end("error");
                return;
            }
            response.end(JSON.stringify(data));
        })
    })
    console.log(idi);
})

// server.listen(81);
// console.log(`server is runing at 81`);

server.listen(port)
console.log("server is running at " + port)